﻿using AgentSystem.AgentCreator.Util;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgentSystem.AgentCreator.Sbpel.Activities
{
    /// <summary>
    /// Activity class for representing assignment.
    /// </summary>
    public class AssignAcivity : AbstractActivity
    {

        #region Properties

        /// <summary>
        /// Source Variable.
        /// </summary>
        public string FromVariableName { get; set; }

        /// <summary>
        /// Destination Variable.
        /// </summary>
        public string ToVariableName { get; set; }

        /// <summary>
        /// Part of the source variable that will be copied.
        /// </summary>
        public string FromVariablePart { get; set; }

        /// <summary>
        /// Part of the destination variable that will have a value set.
        /// </summary>
        public string ToVariablePart { get; set; }

        /// <summary>
        /// Name of the process activity
        /// </summary>
        public string ProcessActivityName { get; set; }

        /// <summary>
        /// Value to be assigned to a variable.
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// Value Type.
        /// </summary>
        public string ValueType { get; set; }

        #endregion

        /// <summary>
        /// Constructor for assign activity.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="fromName"></param>
        /// <param name="fromPart"></param>
        /// <param name="toName"></param>
        /// <param name="toPart"></param>
        public AssignAcivity(AbstractActivity parent, string fromName, string fromPart, string toName, string toPart, string value, string valueType)
            : base(parent)
        {
            FromVariableName = string.Format("{0}.{1}", Constants.StateMemberName,fromName);
            FromVariablePart = fromPart;
            ToVariableName = string.Format("{0}.{1}", Constants.StateMemberName, toName); 
            ToVariablePart = toPart;
            Value = value;
            ValueType = valueType;
        }

        /// <summary>
        /// Getting the C# code for Assingment Activity.
        /// </summary>
        /// <returns></returns>
        public override CodeObject[] GetCodeDom()
        {
            if (string.IsNullOrEmpty(FromVariableName) || string.IsNullOrEmpty(ToVariableName))
            {
                return new CodeObject[] {new CodeCommentStatement("There was an error in the Assign Activity!") };
            }

            CodeAssignStatement assigningStatement = new CodeAssignStatement();

            //Setting Left Member
            if (!string.IsNullOrEmpty(FromVariablePart))
            {
                string leftMember = string.Format("{0}.{1}", FromVariableName, FromVariablePart);
                assigningStatement.Right = new CodeVariableReferenceExpression(leftMember);
            }
            else if (!string.IsNullOrEmpty(Value) && !string.IsNullOrEmpty(ValueType))
            {
                Type type = Type.GetType(ValueType);
                assigningStatement.Right = new CodeCastExpression(type, new CodePrimitiveExpression(Value));
            }
            else
            {
                assigningStatement.Right = new CodeVariableReferenceExpression(FromVariableName);
            }

            //Setting Right Member
            if (string.IsNullOrEmpty(ToVariablePart))
            {
                string rightMember = string.Format("{0}.{1}", ToVariableName, ToVariablePart);
                assigningStatement.Left = new CodeVariableReferenceExpression(rightMember);
            }
            else
            {
                assigningStatement.Left = new CodeVariableReferenceExpression(ToVariableName);
            }

            return new CodeObject[] { assigningStatement };
        }
    }
}
